{% extends 'oratk_app/master.html' %}
{% block tag_left %}
    {% include 'oratk_app/tag_left_user.html' %}
{% endblock %}
{% block title %}
    <title>ORATK</title>
{% endblock %}
{% block userinfo %}
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <form name="Form" role="search" action="/oratk_app/oratk_most_plan_ajax" onsubmit="return validateForm()"
              target="_blank" method="POST">
            <h3 class="page-header">查询sql文本与执行计划</h3>
            <div class="navbar-form navbar-left">
                &nbsp 选择实例: &nbsp
                {#                <select class="form-control" name="conn_string" id="conn_string">#}
                <select class="selectpicker show-tick " style="width:450px" data-live-search="true" name="conn_string"
                        id="conn_string">

                    <option id="def">--请选择实例名--</option>
                    {% for i in instanceinfo_result %}
                        <option id="a">{{ i.dbname }}={{ i.conn_string }}</option>
                    {% endfor %}
                </select>

                <button type="button" class="btn btn-default" id="btSearch">查询(本页展示）</button>
                <button type="submit" class="btn btn-default" id="btSearch_new">查询(新页展示）</button>

                <div>&nbsp</div>
            <div id="args_detail">
                &nbsp 请输入sql文本关键字: &nbsp
                <input type="text" style="width:220px;" name="sql_keyword"  id="sql_keyword"   class="form-control" placeholder="sql文本关键字">
                &nbsp 请输入sqlid: &nbsp
                <input type="text" style="width:220px;" name="sql_id"   id="sql_id"   class="form-control" placeholder="sqlid">
                <div>&nbsp</div>
                <span style="color:red;">&nbsp&nbsp以上参数二选一</span>
            </div>
                <div>&nbsp</div>
{#                <h3 class="page-header">执行结果</h3>#}
                <div id="tab_detail"></div>
            </div>
        </form>
    </div>

    </div>

{% endblock %}

{% block script %}
    <script>
        //新页展示按钮执行前判断输入框是否有空值
        function validateForm() {
            var select_conn = $('#conn_string').find("option:checked").attr("id")
            if (select_conn == 'def') {
                alert('请选择实例')
                return false;
            }
            //判断参数列表输入框是否有空值
            //循环参数列表中所有的input框
            var i = 1
            var arr = []
            $("#args_detail input[type=text]").each(function () {
                var v = $(this).val();
                if (v == '') {
                    val = $(this).attr("val")
                    arr.push(val)
                    i = i + 1
                }
            });
            if (i == 3) {
                alert('参数值不能为空');
                return false;
            }

        }

        //当点击"查询(本页展示)时，输入框值检查及ajax异步调用"
        $('#btSearch').click(function () {
            var select_conn = $('#conn_string').find("option:checked").attr("id")
            if (select_conn == 'def') {
                alert('请选择实例')
                return;
            }

            //判断参数列表输入框是否有空值
            //循环参数列表中所有的input框
            var i = 1
            var arr = []
            var args_list = []
            $("#args_detail input[type=text]").each(function () {
                var v = $(this).val();
                if (v == '') {
                    val = $(this).attr("val")
                    arr.push(val)
                    i = i + 1
                }
            });
            if (i == 3) {
                alert('参数值不能为空');
                return false;
            }
            //发送ajax请求
            $.ajax({
                type: "POST",
                data: {
                    'conn_string': $('#conn_string').val(),
                    'sql_keyword': $('#sql_keyword').val(),
                    'sql_id': $('#sql_id').val(),
                    'submit_id': 0
                },
                url: "/oratk_app/oratk_most_plan_ajax", //后台处理函数的url
                cache: false,
                dataType: "html",
                success: function (result) {
                    $("#tab_detail").html(result);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                },
            })


        })

    </script>

{% endblock %}